/* ----------------------------------------------------------------------------------------------------------------------------------------------- /

ToolWindow - 类名称
修改许可权限 - 修改人名称

< --- Public Vars ------------------------------------------------------------------------------------------------------------------------------ >

< --- Public Functions ------------------------------------------------------------------------------------------------------------------------ >

※ 构造函数
public function ToolWindow()

/ ----------------------------------------------------------------------------------------------------------------------------------------------- */
package editor.ui{
	import com.bit101.components.HSlider;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.List;
	import com.bit101.components.PushButton;
	import com.bit101.components.TextArea;
	import com.bit101.components.Window;
	
	import editor.Notification.V2MNotification;
	import editor.common.AppFacade;
	import editor.common.Common;
	import editor.common.CursorManager;
	import editor.manager.MapManger;
	import editor.map.elements.Npc;
	import editor.map.elements.Teleport;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	public class ToolWindow extends Window{
		
		private var _brushSlider:HSlider;
		private var _brushLabel:Label;
		private var _brushSizeLabel:Label;
		private var _addNpcBtn:PushButton;
		private var _addPortBtn:PushButton;
		private var _npcList:List;
		
		private var _npcIdLabel:Label;
		private var _npcXLabel:Label;
		private var _npcYLabel:Label;
		private var _npcIdInput:InputText;
		private var _setNpcNameBtn:PushButton;
		
		private var _portInfoLabel:Label;
		//传送点说明输入
		private var _portInfoInput:InputText;
		private var _prePortInfo:TextArea;

		private var _selectedNpc:Npc;
		
		public function ToolWindow(parent:DisplayObjectContainer=null, xpos:Number = 0, ypos:Number = 0)
		{
			super(parent, xpos, ypos, "NPC 和 笔刷");
			setSize(120, 285);
			_brushSlider = new HSlider(this, 10, 30);
			_brushSlider.width = 80;
			_brushSlider.maximum = 10;
			_brushSlider.minimum = 1;
			_brushSlider.tick = 1;
			_brushSlider.addEventListener(Event.CHANGE, onBrushSliderChange);
			
			_brushLabel 	= new Label(this, 10, 10, "笔刷大小:");
			_brushSizeLabel = new Label(this, 95, 25, "1");
			
			_addNpcBtn　 = new PushButton(this, 10, 60, "增加NPC", onAddNpc);
			_addNpcBtn.width = 90;
			
//			_addPortBtn = new PushButton(this, 10, 80, "增加传送点", onAddPort);
//			_addPortBtn.width = 90;
			
			_npcList = new List(this, 10, 85);
			_npcList.setSize(90,80);
			_npcList.addEventListener(Event.SELECT, onSelect);
			
			_npcIdLabel = new Label(this, 10, 175, "NPC ID: --");
			_npcXLabel	= new Label(this, 10, 195,"x: ");
			_npcYLabel	= new Label(this, 50, 195,"y: ");
			_npcIdInput　= new InputText(this, 10, 215);
			_npcIdInput.setSize(60,20);
			
			_setNpcNameBtn = new PushButton(this, 80, 215, "Set", clickSetNpcid);
			_setNpcNameBtn.setSize(30, 20);
			
//			_portInfoLabel = new Label(this,10, 260, "传送点说明");
//			_portInfoInput = new InputText(this, 10, 280);
//			
//			_prePortInfo = new TextArea(this, 10, 300, "传送点说明：");
//			_prePortInfo.editable = false;
//			_prePortInfo.setSize(100,60);
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
		}
		
		private function onEnterFrame(evt:Event):void
		{
			if(_selectedNpc is Npc)
			{
				updateNpcInfo(_selectedNpc.npcId, _selectedNpc.x, _selectedNpc.y);
			}
		}
		
		//刷子大小改变
		private function onBrushSliderChange(evt:Event):void
		{
			_brushSizeLabel.text = _brushSlider.value.toString();
			MapManger.getInstance().tileLayer.brushSize = _brushSlider.value;
		}
		
		//增加npc
		private function onAddNpc(evt:Event):void
		{
			Common.cursor.state = CursorManager.STATE_ADD_NPC;
		}
		
		//增加传送点
		private function onAddPort(evt:Event):void
		{
			Common.cursor.state = CursorManager.STATE_ADD_PORT;
		}
		
		//选中一个npc
		private function onSelect(evt:Event):void
		{
			if(_npcList.selectedItem != null)
			{
				setSelectedNpc(Common.map.npcLayer.getNpcByIndex(_npcList.selectedItem.index));
				AppFacade.getIntance().sendNotification(V2MNotification.SET_MAP_VIEW,
					{
						vx:_selectedNpc.x,
						vy:_selectedNpc.y
					});
			}
		}
		
		//设置npc id
		private function clickSetNpcid(evt:Event):void
		{
			if(_selectedNpc != null)
			{
				var id:String = _npcIdInput.text
				id = id.replace(/^\s*/g,"");
				id = id.replace(/\s*$/g,"");

				_selectedNpc.npcId = id;
				_npcIdInput.text = "";
				updateNpcList();
			}
			
			if(_selectedNpc is Teleport && _portInfoInput.text != "")
			{
				(_selectedNpc as Teleport).info = _portInfoInput.text;
				_portInfoInput.text = "";
			}
		}
		
		
		/**
		 * 设置被选中的npc
		 * @param idx 
		 */		
		public function setSelectedNpc(npc:Npc):void
		{
			if(_selectedNpc != null) _selectedNpc.isSelected = false;
			
			_selectedNpc = npc;
			_selectedNpc.isSelected = true;
		}
		
		/**
		 * 更新npc列表 
		 */		
		public function updateNpcList():void
		{
			var list:Vector.<Npc> = Common.map.npcLayer.npcList;
			_npcList.removeAll();
			for(var i:int = 0; i < list.length ; i ++)
			{
				var npc:Npc = list[i];
				if(npc is Teleport)
					_npcList.addItem({label:"传送点 : " + npc.npcId, index:i});
				else 
					_npcList.addItem({label:"NPC  : " + npc.npcId, index:i});
			}
		}
		
		/**
		 * 更新npc信息
		 * @param npcId
		 * @param npcX
		 * @param npcY 
		 */		
		public function updateNpcInfo(npcId:String, npcX:int, npcY:int):void
		{
			_npcIdLabel.text	= "NPC ID: " + npcId;
			_npcXLabel.text 	= "x: " + npcX;
			_npcYLabel.text		= "y: " + npcY;
			_npcIdInput.text;
			_npcIdInput.text;
			
//			if(_selectedNpc is Teleport)
//			    _prePortInfo.text = "传送点说明：\n" + (_selectedNpc as Teleport).info;
		}
		//
	}
}

